﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.Activities;
using CommonLibrary.DataModel.WorkFlow;
using CommonLibrary.Common;
using System.Transactions;
using CommonLibrary.DataModel.Basic;
using ServiceApplication.Dao.WorkFlow;

namespace SharedWorkflows
{
    [ExternalDataExchange]
    public interface IBILLService
    {
        /// <summary>
        /// 通知
        /// </summary>
        void Info2Next(Guid instanceID, UserEntity fqrxx, int[] clrxxs,
            string taskName, object wivList,string clrlx);

        /// <summary>
        /// 打回重新修改
        /// </summary>
        event EventHandler<ReceivedEventArgs> BillUpdated;
        /// <summary>
        /// 向下流转
        /// </summary>
        event EventHandler<ReceivedEventArgs> BillNext;
        /// <summary>
        /// 结束工作流
        /// </summary>
        event EventHandler<ReceivedEventArgs> BillCanceled;
        /// <summary>
        /// 等待
        /// </summary>
        event EventHandler<ReceivedEventArgs> BillWait;
        /// <summary>
        /// 加签
        /// </summary>
        event EventHandler<ReceivedEventArgs> BillInsert;
    }

    /// <summary>
    /// IBILLService 实现，本类在 web项目 <ExternalDataExchangeService/> 节配置加载
    /// </summary>
    [Serializable]
    public class BillServer : IBILLService
    {
        public void Info2Next(Guid instanceID, UserEntity fqrxx, int[] clrxxs, string taskName, object wivList,string clrlx)
        {
            WfInsvalueDao wivDAO = new WfInsvalueDao();
            string fysj = DateUtil.getStandardTimeBySecond();
            for (int i = 0; i < clrxxs.Length; i++)
            {
                WfTaskEntity wfTask = new WfTaskEntity();
                wfTask.uniqueid = instanceID;
                wfTask.taskname = taskName;
                wfTask.state = WfTaskState.TASK_UNDO;
                wfTask.processresult = "";
                wfTask.processtype = clrlx;
                wfTask.processaddition = "";
                wfTask.user_id = clrxxs[i];
                wfTask.fqrcode = fqrxx.id;
                wfTask.starttime = fysj;
                wfTask.checkintime = "";
                wfTask.senttime = DateUtil.getStandardTimeBySecond();
                wfTask.days = 100;

                string taskid = TaskDao.Add(wfTask);
                foreach (var item in (List<WfInsvalueEntity>)wivList)
                {
                    item.task_id = int.Parse(taskid);
                }
                wivDAO.AddEntity((List<WfInsvalueEntity>)wivList);
            }
        }

        Dictionary<string, EventHandler<ReceivedEventArgs>> _EventList = new Dictionary<string, EventHandler<ReceivedEventArgs>>();

        public void RaiseEvent(string name, ReceivedEventArgs receivedEventArgs)
        {
            if (_EventList[name] != null)
            {
                EventHandler<ReceivedEventArgs> eventHand = _EventList[name];
                eventHand(this, receivedEventArgs);
            }
        }

        public static void updateTask(WfTaskEntity wftaskEntity)
        {
            TaskDao.Update(wftaskEntity);
        }

        #region IBILLService 成员
        public event EventHandler<ReceivedEventArgs> BillUpdated
        {
            add { _EventList.Add("BillUpdated", value); }
            remove { _EventList.Remove("BillUpdated"); }
        }
        public event EventHandler<ReceivedEventArgs> BillNext
        {
            add { _EventList.Add("BillNext", value); }
            remove { _EventList.Remove("BillNext"); }
        }
        public event EventHandler<ReceivedEventArgs> BillCanceled
        {
            add { _EventList.Add("BillCanceled", value); }
            remove { _EventList.Remove("BillCanceled"); }
        }
        public event EventHandler<ReceivedEventArgs> BillWait
        {
            add { _EventList.Add("BillWait", value); }
            remove { _EventList.Remove("BillWait"); }
        }
        public event EventHandler<ReceivedEventArgs> BillInsert
        {
            add { _EventList.Add("BillInsert", value); }
            remove { _EventList.Remove("BillInsert"); }
        }
        #endregion
    }

    [Serializable]
    public class ReceivedEventArgs : ExternalDataEventArgs
    {
        public UserEntity Fqrxx{get;set;}
        public int[] Clrxxs { get; set; }
        public string TaskName { get; set; }
        public List<WfInsvalueEntity> WivList { get; set; }

        public ReceivedEventArgs(Guid instanceID, UserEntity fqrxx,int[] clrxxs,string taskName, List<WfInsvalueEntity> wivList)
            : base(instanceID)
        {
            if(fqrxx!=null)
                Fqrxx = fqrxx;
            if(clrxxs!=null)
                Clrxxs = clrxxs;
            TaskName=taskName;
            WivList = wivList;
        }
    }

    [Serializable]
    public class BillModel
    {
        string _userName;
        /// <summary>
        /// 报销人
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        public int UserID { get; set; }

        string _billId;
        /// <summary>
        /// 报销编号，数据库里的
        /// </summary>
        public string BillId
        {
            get { return _billId; }
            set { _billId = value; }
        }

        decimal _money;
        /// <summary>
        /// 报销金额
        /// </summary>
        public decimal Money
        {
            get { return _money; }
            set { _money = value; }
        }

        private string fremark;
        /// <summary>
        /// 财务批示
        /// </summary>
        public string Fremark
        {
            get { return fremark; }
            set { fremark = value; }
        }

        private string bremark;
        /// <summary>
        /// 老板批示
        /// </summary>
        public string Bremark
        {
            get { return bremark; }
            set { bremark = value; }
        }

        private string premark;
        /// <summary>
        /// 出纳批示
        /// </summary>
        public string Premark
        {
            get { return premark; }
            set { premark = value; }
        }

        private string lastRemark;
        /// <summary>
        /// 最后批示
        /// </summary>
        public string LastRemark
        {
            get { return lastRemark; }
            set { lastRemark = value; }
        }

    }

    /// <summary>
    /// 工作的处理者类型
    /// </summary>
    public enum TaskClrType
    {
        按人员,
        按职务,
        发起人,
        由上一步处理人指定
    }
}